package com.qys.livesMall.system.controller.app;

import com.qys.livesMall.common.result.R;
import com.qys.livesMall.system.model.dto.app.SysConfigSaveReq;
import com.qys.livesMall.system.model.entity.H2SysConfig;
import com.qys.livesMall.system.model.vo.app.SysConfigGroupVO;
import com.qys.livesMall.system.service.IH2SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author caoz
 */
@Tag(description = "app-平台配置参数", name = "AppSysConfigController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/h2-sys-config")
@Slf4j
public class AppSysConfigController {

    @Autowired
    private IH2SysConfigService sysConfigService;

    @Operation(description = "查询所有分组以及配置信息")
    @PostMapping("/listGroupConfig")
    public R<List<SysConfigGroupVO>> listGroupConfig() {
        log.info("调用查询所有分组以及配置信息接口");
        return R.ok(sysConfigService.listGroup());
    }

    @Operation(description = "通过分组id查询配置")
    @PostMapping("/listConfigByGroup/{groupId}")
    public R<List<H2SysConfig>> listConfigByGroup(@PathVariable("groupId") int groupId) {
        log.info("调用通过分组id查询配置接口");
        return R.ok(sysConfigService.listConfigByGroup(groupId));
    }

    @Operation(description = "表单数据批量保存接口")
    @PostMapping("/save")
    public R<?> save(@RequestBody SysConfigSaveReq data) {
        log.info("调用表单数据批量保存接口");
        return R.ok(sysConfigService.formSave(data));
    }

    @Operation(description = "通过key查询配置value")
    @PostMapping("/getConfigKey/{key}")
    public R<?> getConfigKey(@PathVariable("key") String key) {
        log.info("通过key查询配置value");
        return R.ok(sysConfigService.getConfigKey(key));
    }


    @Operation(description = "通过key查询配置value")
    @PostMapping("/getConfigKey/list/{key}")
    public R<?> getConfigKeys(@PathVariable("key") String key) {
        log.info("通过key查询配置value");
        List<String> keys=Arrays.stream(key.split(",")).collect(Collectors.toList());
        return R.ok(sysConfigService.getConfigKeys(keys));
    }

    @Operation(description = "查询客服地址")
    @PostMapping("/getConfigServiceAddress")
    public R<List<String>> getConfigServiceAddress() {
        log.info("查询客服地址接口");
        return R.ok(sysConfigService.getConfigServiceAddress());
    }
}
